/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.Timer;

/**
 *
 * @author paintbsd
 */

public class SlideShow_FullScreen {
    JDialog temp1;
    public  JFrame frame;
    JLabel imgView;
    public ArrayList<File>files;
    private static int indexImage;
    private static int DislayTime;
    private static Icon singleFile;
    private static int type;
    public SlideShow_FullScreen(ArrayList<File>arrList,JDialog temp, int strIndexImage,int _DislayTime) {
        files=arrList;
        temp1=temp;
        indexImage = strIndexImage;
        DislayTime=_DislayTime;
        temp1.setVisible(false);
        type=1;
    }
    
    public SlideShow_FullScreen(Icon f,JDialog temp) {
        singleFile=f;
        temp1=temp;
        temp1.setVisible(false);
        type=2;
    }
   
    /**
     * @param args the command line arguments
     */
    public int state;
    int counter = 0;
    //int i=0;
   
    public void AddSlideShow(final int x, final int y) 
    {
        if(type==1)
        {
            final int size = files.size();
            File f=files.get(indexImage);
            ImageIcon imgOriginal=new ImageIcon(f.getPath());
            ImageIcon ico1 = imageSize(imgOriginal, x, y);
            //counter=1;
            indexImage++;
            imgView.setIcon(ico1);
        
            Timer t = new Timer(DislayTime*1000, new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                
                    if (indexImage == size) {
                        indexImage = 0;
                    }
                    File f=files.get(indexImage);
                    ImageIcon imgOriginal=new ImageIcon(f.getPath());
                    ImageIcon ico1 = imageSize(imgOriginal, x, y);
                    imgView.setIcon(ico1);
                    indexImage++;
                }
            });
            t.start();
        }
        else
        {
            imgView.setIcon(singleFile);
        }
    }
    
    public void LoadImage()
    {
        frame = new JFrame();
        //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.toFront();
        frame.setAlwaysOnTop(true);
        frame.getContentPane().setBackground(Color.GRAY);
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Toolkit tk = Toolkit.getDefaultToolkit();  
        int xSize = ((int) tk.getScreenSize().getWidth());  
        int ySize = ((int) tk.getScreenSize().getHeight());  
       
        frame.setSize(xSize,ySize); 
        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent ke) {  // handler
            if(ke.getKeyCode() == KeyEvent.VK_ESCAPE)
                frame.setVisible(false);
                temp1.setVisible(true);
            }  
        });
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                if(state==1)
                    state=2;
                else
                    state=1;
            }
      
        });
        frame.setVisible(true);
        frame.getContentPane().setLayout(new GridBagLayout());
        imgView = new JLabel("");
        imgView.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,1));
        frame.getContentPane().add(imgView);
        AddSlideShow(xSize, ySize);
       
    }
    private ImageIcon resizeIcon(ImageIcon originalIcon, int width, int height) {
        Image img = originalIcon.getImage();
        Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
        return new ImageIcon(newimg);
    }
    private ImageIcon imageSize(ImageIcon imgOriginal, int x, int y) {
        int original_width = imgOriginal.getIconWidth();
        int original_height = imgOriginal.getIconHeight();
        int bound_width = x;
        int bound_height = y;
        int new_width = original_width;
        int new_height = original_height;


        if (original_width > bound_width) {
            new_width = bound_width;
            new_height = (new_width * original_height) / original_width;
        }

        if (new_height > bound_height) {
            new_height = bound_height;
            new_width = (new_height * original_width) / original_height;
        }

        ImageIcon ico1 = resizeIcon(imgOriginal, new_width, new_height);
        return ico1;
    }
}
